package com.qidian.mes.tools.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.pojo.ToolType;
import com.qidian.mes.tools.mapper.ToolsTypeMapper;
import com.qidian.mes.tools.service.ToolsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 任超
 * Time 2024/10/19 13:46
 */
@Service
public class ToolsTypeServiceImpl implements ToolsTypeService {

    @Autowired
    private ToolsTypeMapper toolsTypeMapper;

    @Override
    public MESResult selectList(ToolType toolType, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<ToolType> toolTypes = toolsTypeMapper.list(toolType);
        PageInfo<ToolType> pageInfo = new PageInfo<>(toolTypes);
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult addToolType(ToolType toolType) {
        if (toolsTypeMapper.selectByCode(toolType.getToolTypeCode()) != null)
            return MESResult.error();
        if (toolsTypeMapper.addToolType(toolType)==1)
            return MESResult.success(toolType);
        return MESResult.error();
    }

    @Override
    public MESResult delete(Long toolTypeId) {
        return MESResult.success(toolsTypeMapper.updateToolTypeStatus(toolTypeId));
    }

    @Override
    public MESResult updateType(ToolType toolType) {
        ToolType temp = toolsTypeMapper.selectByCode(toolType.getToolTypeCode());
        if (temp != null && !temp.getToolTypeCode().equals(toolType.getToolTypeCode()))
            return MESResult.error();
        else
            toolsTypeMapper.updateType(toolType);
        return MESResult.success();
    }

    @Override
    public MESResult selectToolType(Long toolTypeId) {
        return MESResult.success(toolsTypeMapper.selectById(toolTypeId));
    }
}
